//
//  MSLColorListView.m
//  ExtModule
//
//  Created by CPX on 2018/7/10.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MSLColorListView.h"

/**
 贺卡字体颜色选择
 */
@interface MSLColorListView()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
{
    NSInteger current_color_index;
}
@property (nonatomic,strong)UICollectionView *  collectionView;
@property (nonatomic,strong)NSMutableArray * dataArray;//字体数据

@end


@implementation MSLColorListView

#pragma mark ------- getter
-(NSMutableArray * )dataArray{
    if (!_dataArray){
        _dataArray = [NSMutableArray new];
        [_dataArray addObject:[UIColor colorWithHexString:@"000000"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"ffffff"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"008643"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"0effc2"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"000080"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"008000"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"abcdf0"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"FF3030"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"ff8800"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"00ff33"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"A52A2A"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"808080"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"FFFF00"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"800080"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"0000FF"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"edaf00"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"b0d844"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"cf438a"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"8B6914"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"00FFFF"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"FF7878"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"9D9D9D"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"FF9730"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"334139"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"FF5234"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"00732F"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"967828"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"0571FA"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"AEEEEE"]];
        [_dataArray addObject:[UIColor colorWithHexString:@"00CDCD"]];
    


        
    }
    return _dataArray;
}
-(UICollectionView* )collectionView{
    if (!_collectionView){
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 20, 20) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.allowsSelection = YES;
        [_collectionView registerClass:GreetColorCell.class forCellWithReuseIdentifier:NSStringFromClass(GreetColorCell.class)];
        
    }
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self  = [super initWithFrame:frame]){
        [self createViews];
        
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    CGSize  size = self.collectionView.size;
    CGFloat width  = (size.width - 10 * 6) / 5;
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.itemSize = CGSizeMake(width, width);
    self.collectionView.collectionViewLayout = layout;
}

-(void)createViews{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
 
    
}


#pragma  mark ----UICollectionViewDelegate,UICollectionViewDataSource,
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GreetColorCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(GreetColorCell.class) forIndexPath:indexPath];

    cell.backgroundColor = self.dataArray[indexPath.row];
    

    
    return cell;
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    current_color_index = indexPath.row;
    if (self.colorSelectEvent)
    {
        UIColor* color = self.dataArray[indexPath.row];
        self.colorSelectEvent(color,current_color_index);
    }
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
#pragma mark ---- 随机生成颜色
-(UIColor * )randomColor{
    int R = (arc4random() % 256) ;
    int G = (arc4random() % 256) ;
    int B = (arc4random() % 256) ;
    
    return [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];
}



@end



@implementation GreetColorCell
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self createViews];
    }
    return self;
}
-(void)createViews
{
    self.stateImg = [UIImageView new];
    [self.contentView addSubview:self.stateImg];

    [self.stateImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        
    }];
}
- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected){
        
    }
    else{
        
    }
}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = self.bounds.size.height/2;
    self.layer.borderColor = [UIColor blackColor].CGColor;
    self.layer.borderWidth = 0.5;
}

@end
